﻿@* Remove this section if you are using bundling *@
@section Scripts {  
        <script type="text/javascript">
            Ext.application({
                name: 'NAVMobile',
                launch: function () {
                    var formPanel = Ext.create('Ext.form.Panel', {
                        fullscreen: true,

                        items: [{
                            xtype: 'fieldset',
                            items: [
                                    {
                                        xtype: 'emailfield',
                                        name: 'email',
                                        label: 'Email'
                                    },
                                    {
                                        xtype: 'passwordfield',
                                        name: 'password',
                                        label: 'Password'
                                    },
                                    {
                                        xtype: 'checkboxfield',
                                        name: 'rememberMe',
                                        label: 'Remember Login'
                                    }
                                ]
                        },
                        {
                            xtype: 'button',
                            text: 'Login',
                            ui: 'confirm',
                            handler: function () {
                                this.up('formpanel').submit({
                                    url: 'Login',
                                    method: 'POST',
                                    success: function () {
                                        alert('form submitted successfully!');
                                    }
                                    , failure: function () {
                                        alert('form submitted fail!');                                        
                                        
                                    }
                                });
                            }
                        }
                        ]
                    });

                }
            });
        </script>
        
}

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Log in";
    
    // Initialize general page variables
    var email = "";
    var password = "";
    var rememberMe = false;

    var returnUrl = Request.QueryString["ReturnUrl"];
    if (returnUrl.IsEmpty()) {
        // Some external login providers always require a return URL value
        returnUrl = Href("~/");
    }

    
    // If this is a POST request, validate and process data
    if (IsPost) {
        //AntiForgery.Validate();
        // is this an external login request?
        string provider = Request.Form["provider"];
       
        {
            email = Request.Form["email"];
            password = Request.Form["password"];
            rememberMe = Request.Form["rememberMe"].AsBool();

            if (WebSecurity.UserExists(email) && WebSecurity.GetPasswordFailuresSinceLastSuccess(email) > 4 && WebSecurity.GetLastPasswordFailureDate(email).AddSeconds(60) > DateTime.UtcNow) {
                Response.Redirect("~/Account/AccountLockedOut");
                return;
            }

            // Attempt to log in using provided credentials
            if (WebSecurity.Login(email, password, rememberMe)) {
                Response.Redirect(returnUrl);
                
            } else {
                ModelState.AddFormError("The user name or password provided is incorrect.");
            }
        }
    }
} 

